package com.sisgesbyte.dao.comun;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.common.exception.BaseException;
import com.sisgesbyte.dao.beanDao.ComunObjetoRequest;
import com.sisgesbyte.data.entity.comun.ComunObjetoEntity;
import com.sisgesbyte.data.entity.comun.ComunObjetoResponseEntity;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoComunObjetoDaoImpl extends AbstractFacade<ComunObjetoEntity> implements
		MantenimientoComunObjetoDao {
	
	
	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantenimientoComunObjetoDaoImpl(){
		super(ComunObjetoEntity.class);
	}

	@Override
	public ComunObjetoResponseEntity guardarComunObjeto( ComunObjetoRequest request) {
		
		String cadena = "";
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT * from comun.sp_registrar_comun_objeto(:in_tipo_objeto_id, :in_tipo_identificacion_id , ");
		jpql.append(":in_num_doc, :in_razon_social ,:in_nombre, :in_apellido, :in_telefono1, :in_direccion, :in_email, ");
		jpql.append(" :in_fecha_nacimiento, :in_pais_id , :in_division_pais_id, :in_ciudad_id, :in_distrito_id )");
		cadena = jpql.toString();
		
		if( request.getTipoObjetoId() < 1){ 
			cadena = cadena.replace(":in_tipo_objeto_id","null");
		}
		if(request.getTipoIdentificacionId() < 1){
			cadena = cadena.replace(":in_tipo_identificacion_id","null"); 
		}
		if(request.getNumDoc() ==null || request.getNumDoc().length() ==0){
			cadena = cadena.replace(":in_num_doc","null"); 
		}
		if(request.getRazonSocial() ==null || request.getRazonSocial().length() ==0){
			cadena = cadena.replace(":in_razon_social","null"); 
		}
		if(request.getNombre() ==null || request.getNombre().length() ==0){
			cadena = cadena.replace(":in_nombre","null"); 
		}
		if(request.getApellido() ==null || request.getApellido().length() ==0){
			cadena = cadena.replace(":in_apellido","null"); 
		}
		if(request.getTelefono1() ==null || request.getTelefono1().length() ==0){ 
			cadena = cadena.replace(":in_telefono1","null");
		}
		if(request.getDireccion() ==null || request.getDireccion().length() ==0){ 
			cadena = cadena.replace(":in_direccion","null");
		}
		if(request.getEmail() ==null || request.getEmail().length() ==0){
			cadena = cadena.replace(":in_email","null"); 
		}
		if(request.getFechaNacimiento() ==null){ 
			cadena = cadena.replace(":in_fecha_nacimiento","null");
		}
		if(request.getPaisId() ==null || request.getPaisId() < 1){
			cadena = cadena.replace(":in_pais_id","null");
		}
		if(request.getDivisionPaisId() ==null || request.getDivisionPaisId() < 1){ 
			cadena = cadena.replace(":in_division_pais_id","null");
		}
		if(request.getCiudadId() ==null || request.getCiudadId() <1){
			cadena = cadena.replace(":in_ciudad_id","null"); 
		}
		if(request.getDistritoId() ==null || request.getDistritoId() <1){
			cadena = cadena.replace(":in_distrito_id","null"); 
		}
		
		Query query = em.createNativeQuery(cadena, ComunObjetoResponseEntity.class);
		if( request.getTipoObjetoId() > 0) {query.setParameter("in_tipo_objeto_id", request.getTipoObjetoId());}
		if(request.getTipoIdentificacionId() > 0 ) {query.setParameter("in_tipo_identificacion_id", request.getTipoIdentificacionId());}
		if(request.getNumDoc() !=null && request.getNumDoc().length() >0) {query.setParameter("in_num_doc", request.getNumDoc());}
		if(request.getRazonSocial() !=null && request.getRazonSocial().length() >0) {query.setParameter("in_razon_social", request.getRazonSocial());}
		if(request.getNombre() !=null && request.getNombre().length() > 0) {query.setParameter("in_nombre", request.getNombre());}
		if(request.getApellido() !=null && request.getApellido().length() > 0) {query.setParameter("in_apellido", request.getApellido());}
		if(request.getTelefono1() !=null && request.getTelefono1().length() >0) {query.setParameter("in_telefono1", request.getTelefono1());}
		if(request.getDireccion() !=null && request.getDireccion().length()>0) {query.setParameter("in_direccion", request.getDireccion());}
		if(request.getEmail() !=null && request.getEmail().length() >0) {query.setParameter("in_email", request.getEmail());}
		if(request.getFechaNacimiento() !=null) {query.setParameter("in_fecha_nacimiento", request.getFechaNacimiento());}
		if(request.getPaisId() !=null && request.getPaisId() > 0) {query.setParameter("in_pais_id", request.getPaisId());}
		if(request.getDivisionPaisId() !=null && request.getDivisionPaisId() > 0) {query.setParameter("in_division_pais_id", request.getDivisionPaisId());}
		if(request.getCiudadId() !=null && request.getCiudadId() >0) {query.setParameter("in_ciudad_id", request.getCiudadId());}
		if(request.getDistritoId() !=null && request.getDistritoId() > 0) {query.setParameter("in_distrito_id", request.getDistritoId());}
			
		return (ComunObjetoResponseEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarComunObjeto(ComunObjetoRequest request) {
		String cadena = "";
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from comun.sp_actualizar_comun_objeto(:in_id, :in_tipo_identificacion_id, ");
		jpql.append(":in_num_doc, :in_razon_social, :in_nombre, :in_apellido, :in_telefono1, :in_direccion, :in_email, ");
		jpql.append(":in_fecha_nacimiento, :in_pais_id, :in_division_pais_id, :in_ciudad_id, :in_distrito_id) ");
		
		cadena = jpql.toString();

		if( request.getId() < 1){ 
			cadena = cadena.replace(":in_id","null");
		}
		if(request.getTipoIdentificacionId() < 1){
			cadena = cadena.replace(":in_tipo_identificacion_id","null"); 
		}
		if(request.getNumDoc() ==null || request.getNumDoc().length() ==0){
			cadena = cadena.replace(":in_num_doc","null"); 
		}
		if(request.getRazonSocial() ==null || request.getRazonSocial().length() ==0){
			cadena = cadena.replace(":in_razon_social","null"); 
		}
		if(request.getNombre() ==null || request.getNombre().length() ==0){
			cadena = cadena.replace(":in_nombre","null"); 
		}
		if(request.getApellido() ==null || request.getApellido().length() ==0){
			cadena = cadena.replace(":in_apellido","null"); 
		}
		if(request.getTelefono1() ==null || request.getTelefono1().length() ==0){ 
			cadena = cadena.replace(":in_telefono1","null");
		}
		if(request.getDireccion() ==null || request.getDireccion().length() ==0){ 
			cadena = cadena.replace(":in_direccion","null");
		}
		if(request.getEmail() ==null || request.getEmail().length() ==0){
			cadena = cadena.replace(":in_email","null"); 
		}
		if(request.getFechaNacimiento() ==null){ 
			cadena = cadena.replace(":in_fecha_nacimiento","null");
		}
		if(request.getPaisId() ==null || request.getPaisId() < 1){
			cadena = cadena.replace(":in_pais_id","null");
		}
		if(request.getDivisionPaisId() ==null || request.getDivisionPaisId() < 1){ 
			cadena = cadena.replace(":in_division_pais_id","null");
		}
		if(request.getCiudadId() ==null || request.getCiudadId() <1){
			cadena = cadena.replace(":in_ciudad_id","null"); 
		}
		if(request.getDistritoId() ==null || request.getDistritoId() <1){
			cadena = cadena.replace(":in_distrito_id","null"); 
		}
		
		System.out.println(cadena);
		
		Query query = em.createNativeQuery(cadena, GenericEntity.class);
			System.out.println("Id:"+request.getId());
			if( request.getId() > 0) {
				query.setParameter("in_id", request.getId());}
			System.out.println(request.getTipoIdentificacionId());
			if(request.getTipoIdentificacionId() > 0 ) {
				query.setParameter("in_tipo_identificacion_id", request.getTipoIdentificacionId());}
			System.out.println(request.getNumDoc());
			if(request.getNumDoc() !=null && request.getNumDoc().length() >0) {
				query.setParameter("in_num_doc", request.getNumDoc());}
			System.out.println(request.getRazonSocial());
			if(request.getRazonSocial() !=null && request.getRazonSocial().length() >0) {
				query.setParameter("in_razon_social", request.getRazonSocial());}
			System.out.println(request.getNombre());
			if(request.getNombre() !=null && request.getNombre().length() > 0) {
				query.setParameter("in_nombre", request.getNombre());}
			System.out.println(request.getApellido());
			if(request.getApellido() !=null && request.getApellido().length() > 0) {
				query.setParameter("in_apellido", request.getApellido());}
			System.out.println(request.getTelefono1());
			if(request.getTelefono1() !=null && request.getTelefono1().length() >0) {
				query.setParameter("in_telefono1", request.getTelefono1());}
			System.out.println(request.getDireccion());
			if(request.getDireccion() !=null && request.getDireccion().length()>0) {
				query.setParameter("in_direccion", request.getDireccion());}
			System.out.println(request.getEmail());
			if(request.getEmail() !=null && request.getEmail().length() >0) {
				query.setParameter("in_email", request.getEmail());}
			System.out.println(request.getFechaNacimiento());
			if(request.getFechaNacimiento() !=null) {
				query.setParameter("in_fecha_nacimiento", request.getFechaNacimiento());}
			System.out.println(request.getPaisId());
			if(request.getPaisId() !=null && request.getPaisId() > 0) {
				query.setParameter("in_pais_id", request.getPaisId());}
			System.out.println(request.getDivisionPaisId());
			if(request.getDivisionPaisId() !=null && request.getDivisionPaisId() > 0) {
				query.setParameter("in_division_pais_id", request.getDivisionPaisId());}
			System.out.println(request.getCiudadId());
			if(request.getCiudadId() !=null && request.getCiudadId() >0) {
				query.setParameter("in_ciudad_id", request.getCiudadId());}
			System.out.println(request.getDistritoId());
			if(request.getDistritoId() !=null && request.getDistritoId() > 0) {
				query.setParameter("in_distrito_id", request.getDistritoId());}

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarComunObjeto(int codObjetoComun) {
		
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from comun.sp_eliminar_comun_objeto(:in_id)");

		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_id", codObjetoComun);

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public Integer guardarObjeto(ComunObjetoRequest request) throws BaseException {
		
		ComunObjetoEntity comun = new ComunObjetoEntity();
		comun.setApellido(request.getApellido());
		comun.setApellido_materno(request.getApellidoMaterno());
		comun.setCiudad_id(request.getCiudadId());
		comun.setDireccion(request.getDireccion());
		comun.setDistrito_id(request.getDistritoId());
		comun.setDivision_pais_id(request.getDivisionPaisId());
		comun.setEmail(request.getEmail());
		comun.setFecha_nacimiento(request.getFechaNacimiento());
		comun.setFecha_registro(request.getFechaRegistro());
		comun.setNombre(request.getNombre());
		comun.setNum_doc(request.getNumDoc());
		comun.setObservacion(request.getObservacion());
		comun.setPais_id(request.getPaisId());
		comun.setRazon_social(request.getRazonSocial());
		comun.setTelefono1(request.getTelefono1());
		comun.setTelefono2(request.getTelefono2());
		comun.setTipo_identificacion_id(request.getTipoIdentificacionId());
		comun.setTipo_objeto_id(request.getTipoObjetoId());
		
		em.persist(comun);
		em.flush();
		
		Integer id = comun.getId();
		
		return id;
	}
	
	

}
